package com.chatai.controller;

import com.chatai.model.ChatMessage;
import com.chatai.model.ChatRoom;
import com.chatai.service.ChatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/chat")
public class ChatController {
    @Autowired
    private ChatService chatService;

    // 创建聊天室
    @PostMapping("/rooms")
    public ResponseEntity<ChatRoom> createChatRoom(@RequestBody ChatRoomRequest request) {
        ChatRoom chatRoom = chatService.createChatRoom(request.getName(), request.getRoomType(), request.getMemberIds());
        return ResponseEntity.ok(chatRoom);
    }
    
    // 获取用户的所有聊天室
    @GetMapping("/rooms")
    public ResponseEntity<List<ChatRoom>> getUserChatRooms() {
        List<ChatRoom> chatRooms = chatService.getUserChatRooms();
        return ResponseEntity.ok(chatRooms);
    }
    
    // 删除聊天室
    @DeleteMapping("/rooms/{chatRoomId}")
    public ResponseEntity<String> deleteChatRoom(@PathVariable Long chatRoomId) {
        chatService.deleteChatRoom(chatRoomId);
        return ResponseEntity.ok("聊天室删除成功");
    }
    
    // 获取聊天室消息历史
    @GetMapping("/rooms/{roomId}/messages")
    public ResponseEntity<List<ChatMessage>> getChatRoomMessages(@PathVariable Long roomId) {
        List<ChatMessage> messages = chatService.getChatRoomMessages(roomId);
        return ResponseEntity.ok(messages);
    }
    
    // 发送消息到聊天室
    @PostMapping("/rooms/{roomId}/messages")
    public ResponseEntity<ChatMessage> sendMessage(@PathVariable Long roomId, @RequestBody MessageRequest request) {
        ChatMessage message = chatService.sendMessage(roomId, request.getContent());
        return ResponseEntity.ok(message);
    }
    
    // 发送消息到聊天室（备用API路径，兼容前端）
    @PostMapping("/rooms/{roomId}/message")
    public ResponseEntity<ChatMessage> sendMessageAlternative(@PathVariable Long roomId, @RequestBody MessageRequest request) {
        // 直接调用相同的服务方法，只是路径不同
        ChatMessage message = chatService.sendMessage(roomId, request.getContent());
        return ResponseEntity.ok(message);
    }
    
    // 获取AI聊天的默认聊天室
    @GetMapping("/ai/room")
    public ResponseEntity<ChatRoom> getAIChatRoom() {
        ChatRoom chatRoom = chatService.getOrCreateAIChatRoom();
        return ResponseEntity.ok(chatRoom);
    }
    
    // 发送消息给AI
    @PostMapping("/ai/response")
    public ResponseEntity<ChatMessage> getAIResponse(@RequestParam Long roomId, @RequestParam String query) {
        ChatMessage response = chatService.getAIResponse(roomId, query);
        return ResponseEntity.ok(response);
    }
    
    // 请求参数内部类
    static class ChatRoomRequest {
        private String name;
        private String roomType; // 'private', 'group', 'ai'
        private Set<Long> memberIds;
        
        // Getters and Setters
        public String getName() { return name; }
        public void setName(String name) { this.name = name; }
        public String getRoomType() { return roomType; }
        public void setRoomType(String roomType) { this.roomType = roomType; }
        public Set<Long> getMemberIds() { return memberIds; }
        public void setMemberIds(Set<Long> memberIds) { this.memberIds = memberIds; }
    }
    
    static class MessageRequest {
        private String content;
        
        // Getters and Setters
        public String getContent() { return content; }
        public void setContent(String content) { this.content = content; }
    }
}